{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tempfile import mkstemp\n",
    "from shutil import move, copymode\n",
    "from os import fdopen, remove\n",
    "import timeit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_case = '../test/V1/input.kmc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "mkdir: cannot create directory ‘temp_case/’: File exists\n"
     ]
    }
   ],
   "source": [
    "!mkdir temp_case/\n",
    "!cp ../test/V1/* ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def replace(file_path, pattern, subst):\n",
    "    #Create temp file\n",
    "    fh, abs_path = mkstemp()\n",
    "    with fdopen(fh,'w') as new_file:\n",
    "        with open(file_path) as old_file:\n",
    "            for line in old_file:\n",
    "                if pattern in line:\n",
    "                    new_file.write(subst)\n",
    "                else:\n",
    "                    new_file.write(line)\n",
    "    #Copy the file permissions from the old file to the new file\n",
    "    copymode(file_path, abs_path)\n",
    "    #Remove original file\n",
    "    remove(file_path)\n",
    "    #Move new file\n",
    "    move(abs_path, file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_iters = 100\n",
    "\n",
    "write_iter = \"\\\"Iterations\\\": \" + str(num_iters) +\",\\n\"\n",
    "replace(\"temp_case/input.kmc\", 'Iterations', write_iter)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "input.kmc successfully opened.\n",
      "Successfully parsed input file\n",
      "lattice_type FCC\n",
      "Building the lattice\n",
      "Finished building the lattice\n",
      "Initiating Apothesis\n",
      "Opening output file\n",
      "Adsorption\n",
      "\n",
      "\n",
      "Executing Apothesis\n",
      "Adsorption process is being performed...\n",
      "\n",
      "real\t0m0.051s\n",
      "user\t0m0.016s\n",
      "sys\t0m0.031s\n"
     ]
    }
   ],
   "source": [
    "\n",
    "!cd temp_case/ & time /home/deifilia/Apothesis/src/build/Apothesis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}